package com.tuanzi.loan.business.repository.statistics.impl;

import com.tuanzi.loan.business.repository.statistics.WholeProcessPageUvStatRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.statistics.WholeProcessPageuvstatSearchRequest;
import com.tuanzi.loan.vo.response.statistics.WholeProcessPageuvstatSearchResponse;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.Objects;

import static com.tuanzi.loan.generated.jooq.stat.tables.StatWholeProcessPage.STAT_WHOLE_PROCESS_PAGE;

@Repository
public class WholeProcessPageUvStatRepositoryImpl extends JOOQBaseRepositoryPlus implements WholeProcessPageUvStatRepository{
    @Override
    protected TableImpl getTable() {
        return STAT_WHOLE_PROCESS_PAGE;
    }

    @Override
    public Page<WholeProcessPageuvstatSearchResponse> list(WholeProcessPageuvstatSearchRequest searchRequest, Pageable pageable) {
        fields();
        if (Objects.nonNull(searchRequest.getChannel())){
            eq(STAT_WHOLE_PROCESS_PAGE.CHANNEL,searchRequest.getChannel());
        }
        if (Objects.nonNull(searchRequest.getProId())){
            eq(STAT_WHOLE_PROCESS_PAGE.PRDID,searchRequest.getProId());
        }
        if (Objects.nonNull(searchRequest.getActivity_name())){
            eq(STAT_WHOLE_PROCESS_PAGE.ACTIVITY_NAME,searchRequest.getActivity_name());
        }
        if (Objects.nonNull(searchRequest.getStartDate())){
            ge(STAT_WHOLE_PROCESS_PAGE.TIME,searchRequest.getStartDate());
        }

        if (Objects.nonNull(searchRequest.getEndDate())){
            le(STAT_WHOLE_PROCESS_PAGE.TIME,searchRequest.getEndDate());
        }

        if (Objects.isNull(searchRequest.getEndDate())&&Objects.isNull(searchRequest.getStartDate())){
            between(STAT_WHOLE_PROCESS_PAGE.TIME,searchRequest.getStartDate(),searchRequest.getEndDate());
        }

        return search(pageable,record -> {
            WholeProcessPageuvstatSearchResponse response= new WholeProcessPageuvstatSearchResponse();
            response.setActivity_name(record.get(STAT_WHOLE_PROCESS_PAGE.ACTIVITY_NAME));
            response.setChannel(record.get(STAT_WHOLE_PROCESS_PAGE.CHANNEL));
            response.setConfirm_sign_uv(record.get(STAT_WHOLE_PROCESS_PAGE.CONFIRM_SIGN_UV));
            response.setFirst_inner_order_uv(record.get(STAT_WHOLE_PROCESS_PAGE.FIRST_INNER_ORDER_UV));
            response.setIntention_commit_uv(record.get(STAT_WHOLE_PROCESS_PAGE.INTENTION_COMMIT_UV));
            response.setProid(record.get(STAT_WHOLE_PROCESS_PAGE.PRDID));
            response.setReal_name_next_uv(record.get(STAT_WHOLE_PROCESS_PAGE.REAL_NAME_NEXT_UV));
            response.setReal_name_uv(record.get(STAT_WHOLE_PROCESS_PAGE.REAL_NAME_UV));
            response.setSign_page_uv(record.get(STAT_WHOLE_PROCESS_PAGE.SIGN_PAGE_UV));
            response.setWait_money_uv(record.get(STAT_WHOLE_PROCESS_PAGE.WAIT_MONEY_UV));
            response.setTime(record.get(STAT_WHOLE_PROCESS_PAGE.TIME));
            response.setId(record.get(STAT_WHOLE_PROCESS_PAGE.ID));
            response.setTied_card_page_uv(record.get(STAT_WHOLE_PROCESS_PAGE.TIED_CARD_PAGE_UV));
            response.setSuccess_money_uv(record.get(STAT_WHOLE_PROCESS_PAGE.SUCCESS_MONEY_UV));
            response.setSubmit_information_uv(record.get(STAT_WHOLE_PROCESS_PAGE.SUBMIT_INFORMATION_UV));
            response.setSubmit_commit_success_uv(record.get(STAT_WHOLE_PROCESS_PAGE.SUBMIT_COMMIT_SUCCESS_UV));
            response.setSign_success_uv(record.get(STAT_WHOLE_PROCESS_PAGE.SIGN_SUCCESS_UV));
            response.setIntention_page_uv(record.get(STAT_WHOLE_PROCESS_PAGE.INTENTION_PAGE_UV));
            response.setSubmit_information_success_uv(record.get(STAT_WHOLE_PROCESS_PAGE.SUBMIT_INFORMATION_SUCCESS_UV));
            return response;
        });
    }
}
